﻿package com.societyGames.system
{
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	/**
	 * ...
	 * @author Alexander Jhin
	 */
	public class EventObject
	{
		private var _eventDispatcher:IEventDispatcher; //The event dispatcher to raise of the event on.
		private var _type:String;	//The string type of the event.
		private var _event:Event; //A stored event so we don't keep allocating one.
		public function EventObject(eventDispatcher:IEventDispatcher, type:String, eventClass:Class = null)
		{
			this._eventDispatcher = eventDispatcher;
			this._type = type;
			if (eventClass != null)
			{
				this._event = new eventClass(this._type);
			}
			else
			{
				this._event = new Event(this._type);
			}
		}
		
		/// Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event.
		public function addEventListener (listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false) : void
		{
			this._eventDispatcher.addEventListener(this._type, listener, useCapture, priority, useWeakReference);
		}

		/// Dispatches an event into the event flow.
		public function dispatchEvent (event:Event = null) : Boolean
		{
			if (event == null)
			{
				return this._eventDispatcher.dispatchEvent(this._event);
			}
			else
			{
				return this._eventDispatcher.dispatchEvent(event);
			}
		}

		/// Checks whether the EventDispatcher object has any listeners registered for a specific type of event.
		public function hasEventListener () : Boolean
		{
			return this._eventDispatcher.hasEventListener(this._type);
		}

		/// Removes a listener from the EventDispatcher object.
		public function removeEventListener (listener:Function, useCapture:Boolean = false) : void
		{
			this._eventDispatcher.removeEventListener(this._type, listener, useCapture);
		}

		/// Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type.
		public function willTrigger (type:String) : Boolean
		{
			return this._eventDispatcher.willTrigger(this._type);
		}
		
		public function get type():String { return _type; }
	}
}